package top.wu2020.lazy.ws.chat.application.impl;

import jakarta.annotation.Resource;
import org.wu.framework.database.lazy.web.plus.stereotype.LazyApplication;
import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;
import org.wu.framework.web.response.Result;
import top.wu2020.lazy.ws.chat.application.UserApplication;
import top.wu2020.lazy.ws.chat.application.assembler.UserDTOAssembler;
import top.wu2020.lazy.ws.chat.application.command.user.*;
import top.wu2020.lazy.ws.chat.application.dto.UserDTO;
import top.wu2020.lazy.ws.chat.domain.model.user.User;
import top.wu2020.lazy.ws.chat.domain.model.user.UserRepository;

import java.util.List;
import java.util.stream.Collectors;
/**
 * describe user 
 *
 * @author Jia wei Wu
 * @date 2024/05/24 08:52 晚上
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyApplicationImpl 
 **/
@LazyApplication
public class UserApplicationImpl implements UserApplication {

    @Resource
    UserRepository userRepository;
    /**
     * describe 新增
     *
     * @param userStoryCommand 新增     
     * @return {@link Result<User>} 新增后领域对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<User> story(UserStoryCommand userStoryCommand) {
        User user = UserDTOAssembler.INSTANCE.toUser(userStoryCommand);
        return userRepository.story(user);
    }
    /**
     * describe 批量新增
     *
     * @param userStoryCommandList 批量新增     
     * @return {@link Result<List<User>>} 新增后领域对象集合     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<List<User>> batchStory(List<UserStoryCommand> userStoryCommandList) {
        List<User> userList = userStoryCommandList.stream().map( UserDTOAssembler.INSTANCE::toUser).collect(Collectors.toList());
        return userRepository.batchStory(userList);
    }
    /**
     * describe 更新
     *
     * @param userUpdateCommand 更新     
     * @return {@link Result<User>} 领域对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<User> updateOne(UserUpdateCommand userUpdateCommand) {
        User user = UserDTOAssembler.INSTANCE.toUser(userUpdateCommand);
        return userRepository.story(user);
    }

    /**
     * describe 查询单个
     *
     * @param userQueryOneCommand 查询单个     
     * @return {@link Result<UserDTO>} DTO对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<UserDTO> findOne(UserQueryOneCommand userQueryOneCommand) {
        User user = UserDTOAssembler.INSTANCE.toUser(userQueryOneCommand);
        return userRepository.findOne(user).convert(UserDTOAssembler.INSTANCE::fromUser);
    }

    /**
     * describe 查询多个
     *
     * @param userQueryListCommand 查询多个     
     * @return {@link Result<List<UserDTO>>} DTO对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<List<UserDTO>> findList(UserQueryListCommand userQueryListCommand) {
        User user = UserDTOAssembler.INSTANCE.toUser(userQueryListCommand);
        return userRepository.findList(user)        .convert(users -> users.stream().map(UserDTOAssembler.INSTANCE::fromUser).collect(Collectors.toList())) ;
    }

    /**
     * describe 分页查询多个
     *
     * @param userQueryListCommand 分页查询多个     
     * @return {@link Result<LazyPage<UserDTO>>} 分页DTO对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<LazyPage<UserDTO>> findPage(int size,int current,UserQueryListCommand userQueryListCommand) {
        User user = UserDTOAssembler.INSTANCE.toUser(userQueryListCommand);
        return userRepository.findPage(size,current,user)        .convert(page -> page.convert(UserDTOAssembler.INSTANCE::fromUser))            ;
    }

    /**
     * describe 删除
     *
     * @param userRemoveCommand 删除     
     * @return {@link Result<User>}      
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<User> remove(UserRemoveCommand userRemoveCommand) {
     User user = UserDTOAssembler.INSTANCE.toUser(userRemoveCommand);
     return userRepository.remove(user);
    }

    /**
     * 获取随机用户
     *
     * @return
     */
    @Override
    public Result<UserDTO> getRandomUser() {
        Result<User> randomUser= userRepository.getRandomUser();
        return randomUser.convert(UserDTOAssembler.INSTANCE::fromUser);
    }

    /**
     * 登录
     *
     * @param userLoginCommand
     * @return
     */
    @Override
    public Result<UserDTO> login(UserLoginCommand userLoginCommand) {
        User user = new User();
        user.setEmail(userLoginCommand.getEmail());
        user.setPassword(userLoginCommand.getPassword());
        Result<User> one = userRepository.findOne(user);
        return one.convert(UserDTOAssembler.INSTANCE::fromUser);
    }
}